package portal.action.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
//@Deprecated
public class RequestPermissionInterceptor extends HandlerInterceptorAdapter{

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		super.afterCompletion(request, response, handler, ex);
	}

	@Override
	public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		super.afterConcurrentHandlingStarted(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		//return true;
		// TODO Auto-generated method stub
		//super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		if(request.getRequestURI().contains("login")||request.getRequestURI().contains("/ws/source")||request.getRequestURI().contains("/ws/remote"))
			return true;

		if(request.getSession()==null)
			return false;
		HttpSession session =request.getSession();
		String userCode=(String)session.getAttribute("userCode");
		if(userCode==null)
			return false;
		
		if(!haseUrlPermission(request.getRequestURI(),userCode)){
			response.sendRedirect(request.getContextPath()+"/security/security.do");
			return false;
		}
		return true;
		//return super.preHandle(request, response, handler);
	}
	private boolean haseUrlPermission(String url, String userCode) {
//		Log4jInit.logger.info(url);
//		Log4jInit.logger.info(userCode);
//		Log4jInit.logger.info(url);
//		Log4jInit.logger.info(userCode);
		if(url.contains(".html")){
			if( url.contains("index.html"))
					return true;
			if( (url.contains( "algorithmList.html") && userCode.contains( "1001")) 
				 || (url.contains( "algorithmRegister.html") && userCode.contains( "1001"))
				 || (url.contains( "algorithmXMLRegister.html") && userCode.contains( "1001")))
					return true;
			if( (url.contains( "modelEdit.html") && userCode.contains( "1002")) 
				 || (url.contains( "modelList.html") && userCode.contains( "1002")))
					return true;
			if( (url.contains( "tasksList.html")&& userCode.contains( "1003")) 
				 || (url.contains( "tasksHistoryList.html")&& userCode.contains( "1003")))
					return true;
			if( (url.contains( "algorithmCompany.html") && userCode.contains( "1004"))
				 || (url.contains( "usersManagement.html") && userCode.contains( "1004"))
			    || (url.contains( "algorithmClassification.html")) && (userCode.contains( "1004"))
			    || (url.contains( "logList.html")) && (userCode.contains( "1004")))
			    	return true;
			if( (url.contains( "storage.html")) && (userCode.contains( "1005")))
					return true;
			if( (url.contains( "workRegister.html") && userCode.contains( "1006"))
				 || (url.contains( "workList.html") && userCode.contains( "1006"))
			    || (url.contains( "queueRegister.html") && userCode.contains( "1006"))
			    || (url.contains( "queueList.html") && userCode.contains( "1006"))
			    || (url.contains( "nodeRegister.html") && userCode.contains( "1006"))
			    || (url.contains( "nodeList.html") && userCode.contains( "1006"))
			    || (url.contains( "nodeClassification.html") && userCode.contains( "1006"))
			    || (url.contains( "gpuList.html") && userCode.contains( "1006"))
			    || (url.contains( "clusterMonitoring.html") && userCode.contains( "1006")))
			    return true;
			
			return false;
		}
	 return true;

	}
	

}
